#include <stdarg.h>
#include "biscuit.h"
#include "putfunc.h"
// int
// printk(const char *format, ...) {

// } // done
// Print to the console. only understands %d, %x, %p, %s.
void panic (char * msg);

void
printk(char *fmt, ...)
{
  va_list ap;
  int i, c;
  char *s;
  if (fmt == 0)
    panic("null fmt");

  va_start(ap, fmt);
  for(i = 0; (c = fmt[i] & 0xff) != 0; i++){
    if(c != '%'){
      putc(c);
      continue;
    }
    c = fmt[++i] & 0xff;
    if(c == 0)
      break;
    switch(c){
    case 'd':
      putd(va_arg(ap, int));
      break;
    case 'x':
      putx(va_arg(ap, int));
      break;
    case 'p':
      putp(va_arg(ap, void*));
      break;
    case 's':
      if((s = va_arg(ap, char*)) == 0)
        s = "(null)";
      for(; *s; s++)
        putc(*s);
      break;
    case '%':
      putc('%');
      break;
    default:
      // Print unknown % sequence to draw attention.
      putc('%');
      putc(c);
      break;
    }
  }
}

void
panic (char * msg) {
    printk ("oops.. system panicked: %s",msg);
    ll_halt ();
}